﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{   
    public class ResearchDivisionsDataAccess : DivisionsDataAccess
    {
        public ResearchDivisionsDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<ResearchDivision> SelectResearchDivisionsList()
        {
            List<Division> divList = SelectDivisionsList();

            List<RESEARCH_DIVISION> resDivList = dataContext.RESEARCH_DIVISION.ToList();

            //Merge result
            var res = (from div in divList
                       join resDiv in resDivList on div.ID equals resDiv.ID
                       select new ResearchDivision
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           FINANCING = (Decimal)resDiv.FINANCING,
                           PURPOSE = resDiv.PURPOSE,
                           SPONSORS = resDiv.SPONSORS,
                           AWARDS = resDiv.AWARDS
                       }).ToList();

            return res;
        }

        public ResearchDivision SelectResearchDivisionBy_Id(int Id)
        {
            try
            {
                Division basePart = SelectDivisionBy_Id(Id);

                List<RESEARCH_DIVISION> resDivisionPart = dataContext.RESEARCH_DIVISION.ToList();

                //Merge result
                var res = (from resDiv in resDivisionPart
                           where resDiv.ID == basePart.ID
                           select new ResearchDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               FINANCING = (Decimal)resDiv.FINANCING,
                               PURPOSE = resDiv.PURPOSE,
                               SPONSORS = resDiv.SPONSORS,
                               AWARDS = resDiv.AWARDS
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectResearchDivisionById");
            }
        }

        public void InsertResearchDivision(ResearchDivision resDivision, ref int generated_Id)
        {
            try
            {
                InsertDivision(resDivision, ref generated_Id);

                int new_Id = generated_Id;
                RESEARCH_DIVISION resDivisionPart = new RESEARCH_DIVISION();
                resDivisionPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                resDivisionPart.ID = new_Id;
                resDivisionPart.FINANCING = resDivision.FINANCING;
                resDivisionPart.PURPOSE = resDivision.PURPOSE;
                resDivisionPart.SPONSORS = resDivision.SPONSORS;
                resDivisionPart.AWARDS = resDivision.AWARDS;

                //Submit changes
                dataContext.AddToRESEARCH_DIVISION(resDivisionPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertResearchDivision");
            }
        }

        public void UpdateResearchDivision(ResearchDivision resDivision)
        {
            try
            {
                UpdateDivision(resDivision);

                RESEARCH_DIVISION resDivisionPart = dataContext.RESEARCH_DIVISION.Where(t => t.ID == resDivision.ID).FirstOrDefault();

                resDivisionPart.FINANCING = resDivision.FINANCING;
                resDivisionPart.PURPOSE = resDivision.PURPOSE;
                resDivisionPart.SPONSORS = resDivision.SPONSORS;
                resDivisionPart.AWARDS = resDivision.AWARDS;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateResearchDivision");
            }
        }

        public void DeleteResearchDivision(ResearchDivision resDivision)
        {
            try
            {
                RESEARCH_DIVISION resDivPart = dataContext.RESEARCH_DIVISION.Where(t => t.ID == resDivision.ID).FirstOrDefault();

                dataContext.DeleteObject(resDivPart);
                dataContext.SaveChanges();


                DeleteDivision(resDivision);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteResearchDivision");
            }
        }
    }
}
